<?php 
    session_start();
    ob_start();
    class LoginController{
        public function handleRequest(){
            $user_model=new UserTableModel();
            $permission_model =new PermissionTableModel();
            if((isset($_SESSION['bg_login']['bg_username']) && isset($_SESSION['bg_login']['bg_level']) && $_SESSION['bg_login']['bg_level'] == 1)
                 || (isset($_COOKIE['bg_username'])&& isset($_COOKIE['bg_level']) && $_COOKIE['bg_level'] == 1 )){
                header("Location:".BASE_URL_ADMIN);
            }else{
                if(isset($_POST['login'])){
                    $error = array();
                    if($_POST['userName'] == null){
                        $error[] = "Please Enter username";
                    }
                    if($_POST['passWord'] == null){
                        $error[] = "Please enter password";
                    }
                    if(empty($error)){
                        $messege = array();
                        $username = $_POST['userName'];
                        $password = $_POST['passWord'];
                        $remmember = $_POST['rememberMe'];
                        $value = $user_model->login($username,$password);
                        if($value == 1){
                            $info = $user_model->getInfoByUserName($username);
                            
                            if($remmember == "on"){   
                                setcookie('bg_username',$info[0]['user_name'],time()+3600);
                                setcookie('bg_username',$info[0]['user_name'],time()+3600);
                                $_SESSION['bg_login']['bg_username'] = $info[0]['user_name'];
                                $_SESSION['bg_login']['bg_level'] = 1;
                                $_SESSION['bg_login']['bg_user_id'] = $info[0]['user_id'];
                               
                            }else{
                                $_SESSION['bg_login']['bg_username'] = $info[0]['user_name'];
                                $_SESSION['bg_login']['bg_level'] = 1;
                                $_SESSION['bg_login']['bg_user_id'] = $info[0]['user_id'];
                            }
                            if($info[0]['user_id'] != 1){
                                $permission = $permission_model->getPermissionByUserID($info[0]['user_id']);
                                foreach($permission as $pm){
                                    $permission_component_name = $pm['permission_component_name'];
                                    $permission_name = $pm['permission_name'];
                                    $_SESSION["'".$permission_component_name."'"]["'".$permission_name."'"] = $pm['permission_status'];
                                }
                                
                            }
                            if(isset($_REQUEST['action']) && !empty($_REQUEST['action'])){
                                 header("Location:".$_REQUEST['action']);
                            }else{
                                header("Location:".BASE_URL_ADMIN);
                            }

                        }else if($value == 2){
                            $error[] = "Wrong username";
                        }else if($value == 3){
                            $error[] = "Wrong password";
                        }else{
                            $error[] = "Do not have access";
                        }
                    }
                }
                
               	render_admin("index",array("errors"=>$error),"login/");
            }
		}
	}
?>